package view.component.asset
{
import flash.display.MovieClip;

import framework.core.AssetsManager;
import framework.proxy.ComponentProxy;
import framework.view.components.graphics.Asset;
import framework.view.components.graphics.SWF;

/**
 * SWFProxy
 * @author jliang
 *
 */
public class SwfProxy extends ComponentProxy
{
	//==========================================================================
	//  Properties
	//==========================================================================
	protected var _url:String;
	
	public function get url():String
	{
		return _url;
	}
	
	protected var _completeHandler:Function;
	
	public function get completeHandler():Function
	{
		return _completeHandler;
	}
	
	protected var _asset:SWF;
	
	public function get asset():SWF
	{
		return _asset;
	}
	
	protected var _content:MovieClip;

	public function get content():MovieClip
	{
		return _content;
	}
	
	protected var _ref:String;

	public function get ref():String
	{
		return _ref;
	}
	//==========================================================================
	//  Overridden methods: ComponentProxy
	//==========================================================================
	override public function dispose():void
	{
		stop();
		
		super.dispose();		
	}
	//==========================================================================
	//  Public methods
	//==========================================================================
	public function load(url:String, completeHandler:Function = null, mode:String = "single", ref:String = null):void
	{
		stopAndRemove();
		
		this._url = url;
		this._completeHandler = 	completeHandler;	
		this._ref = ref;
		
		_asset = AssetsManager.loadAsset(url, loadedHandler, errorHandler, mode) as SWF;
	}
	
	public function stop():void
	{
		if (asset)
		{
			asset.dispose();
			_asset = null;
		}
		
		_url = null;
		_completeHandler = null;
	}
	
	public function stopAndRemove():void
	{
		if (content && content.parent)
		{
			content.stop();
			content.parent.removeChild(content);
		}
		stop();
	}
	
	protected function loadedHandler(asset:SWF):void
	{
		if (asset.mode == SWF.EXTERNAL_LIBS)
		{
			var classRef:Class = asset.getClassRef(ref);
			_content = new classRef() as MovieClip;
		}
		else
		{
			_content = asset.content as MovieClip;
		}
		
		skin["assetHolder"].addChild(content);
		
		if (completeHandler != null)
		{
			completeHandler(this);
		}
	}
	
	protected function errorHandler(asset:Asset):void
	{
		
	}
}
}



